<?php
/**
 * File : UserFormGenerator.php
 * Created by Ludovic Reenaers
 * Created on  26 oct. 2010
 * lreenaers@hotmail.com
 */
class UserFormGenerator extends Generator implements Wrapable{
	protected $children = array();


	function setUp(){
			
		if($_SESSION['USER_BEAN']->getProfile()->getLabel()=='anonymous' || $_SESSION['action']=="add"){
			$name = "";
			$surname = "";
			$email = "";
			$rue = "";
			$num = "";
			$city = "";
			$zip = "";
			$country = "";
			$ddn = "";
			$url = $_SESSION['config']->controler."?/user/insert/";
			$updtBut=$this->translator->userAdd;
		}elseif ($_SESSION['action']=="edit") {
			$updtBut=$this->translator->userEdit;
			$name = $_SESSION['USER_BEAN']->getName();
			$surname = $_SESSION['USER_BEAN']->getSurname();
			$email = $_SESSION['USER_BEAN']->getEmail();
			$rue = $_SESSION['USER_BEAN']->getStreet();
			$num = $_SESSION['USER_BEAN']->getNumber();
			$city = $_SESSION['USER_BEAN']->getCity();
			$zip = $_SESSION['USER_BEAN']->getZip();
			$country = $_SESSION['USER_BEAN']->getCountry();
			$url = $_SESSION['config']->controler."?/user/update/";
			list($year,$month,$day)=split('[/,-]',$_SESSION['USER_BEAN']->getDdn());
			$ddn = $day."/".$month."/".$year;
		}
	  
		$lngGen = new LngListGenerator($this->translator,$this->params,$this->Factory);

		$lng = $lngGen->dump();
	  
		$labels = array('{updtbutton}','{encoding}','{ERROR-HERE}','{labelNom}','{nomValue}','{nominfo}','{url}');
		$labels2 = array('{labelprenom}','{prenomvalue}','{prenominfo}', '{labelemail}','{emailvalue}','{emailinfo}');
		$labels3 = array('{labelrue}','{ruevalue}','{rueinfo}', '{labelnumero}','{numerovalue}','{numeroinfo}');
		$labels4 = array('{labelcity}','{cityvalue}','{cityinfo}', '{labelzip}','{zipvalue}','{zipinfo}');
		$labels5 = array('{labelcountry}','{countryvalue}','{countryinfo}', '{labelddn}','{ddnvalue}','{ddninfo}');
		$labels6 = array('{labellng}','{lnglist}','{labelPws}','{pwsinfo}','{labelPws2}','{pws2info}','{title}');
	  
		$values = array($updtBut,$_SESSION['encoding'],$this->error,$this->translator->labelNom,$name,$this->translator->nomInfo,$url);
		$values2 = array($this->translator->labelPrenom,$surname,$this->translator->prenomInfo,$this->translator->labelEmail,$email,$this->translator->emailInfo);
		$values3 = array($this->translator->labelRue,$rue,$this->translator->rueInfo,$this->translator->labelNumero, $num, $this->translator->numeroInfo);
		$values4 = array($this->translator->labelCity,$city,$this->translator->cityInfo,$this->translator->labelZip,$zip,$this->translator->zipInfo);
		$values5 = array($this->translator->labelCountry,$country,$this->translator->countryInfo,$this->translator->labelDDN,$ddn,$this->translator->ddnInfo);
		$values6 = array($this->translator->labelLng,$lng,$this->translator->labelPws,$this->translator->pwsinfo,$this->translator->labelPws2,$this->translator->pws2info,$_SESSION['catname']);
			


		$template = file_get_contents("html/templates/userEdit");
		$template = str_replace($labels,$values,$template);
		$template = str_replace($labels2,$values2,$template);
		$template = str_replace($labels3,$values3,$template);
		$template = str_replace($labels4,$values4,$template);
		$template = str_replace($labels5,$values5,$template);
		$template = str_replace($labels6,$values6,$template);
		$this->content .= $template;
	}


	public function setChildren($childArray){
		$bool = true;
		foreach ($childArray as $child){
			if (!is_a( $child, "Generator")){
				$bool = false;
			}
		}
		if($bool){
			$this->children = $childArray;
		}else{
			throw new Exception ("Array must only contains Generators.!");
		}
	}
	public function getChildren(){
		return $this->children;
	}
	public function addChild($Child){
		if (is_a($Child, "Generator")){
			array_push($this->children, $Child);
		}else{
			throw new Exception("you can only add Generators.");
		}
	}
	function dump(){
		$this->setUp();
		$wraped = "";
			
		foreach ($this->children as $child){

			$wraped.=$child->dump();
		}
		return str_replace("{WRAP-HERE}",$wraped,$this->content);
	}
}
?>